package logic

import (
	"context"
	"shop_srv/shop"

	"api/internal/svc"
	"api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type CreateOrderLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewCreateOrderLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateOrderLogic {
	return &CreateOrderLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *CreateOrderLogic) CreateOrder(req *types.CreateOrderrequest) (resp *types.Response, err error) {
	order, err := l.svcCtx.Shoprpc.CreateOrder(l.ctx, &shop.CreateOrderRequest{
		UserId:     int64(req.UserId),
		GoodsId:    int64(req.GoodsId),
		OrderPrice: float32(req.OrderPrice),
	})
	if err != nil {
		return &types.Response{
			Code:    500,
			Message: err.Error(),
			Data:    nil,
		}, nil
	}

	return &types.Response{
		Code:    200,
		Message: "生成订单成功",
		Data:    order,
	}, nil
}
